﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XYW.WebSite;

namespace xyw
{
    public partial class NewsList : System.Web.UI.Page
    {
        string id = SiteAbout.gStr("id");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            string id = SiteAbout.gStr("id");
            XYW.BLL.News bllNews = new XYW.BLL.News();
            //this.Repeater1.DataSource = bllNews.GetList("classify='" + id + "'");
            //this.Repeater1.DataBind();
            UsersBind(bllNews.GetList("classify='" + id + "' order by id desc").Tables[0], 15);
        }

        private void UsersBind(DataTable userList, int pageSize)//repeater分页并绑定
        {
            try
            {
                PagedDataSource objPage = new PagedDataSource();
                objPage.DataSource = userList.DefaultView;
                objPage.AllowPaging = true;
                objPage.PageSize = pageSize;
                int CurPage;
                if (Request.QueryString["page"] != null)
                {
                    CurPage = Convert.ToInt32(Request.QueryString["page"]);
                }
                else
                {
                    CurPage = 1;
                }
                objPage.CurrentPageIndex = CurPage - 1;
                Repeater1.DataSource = objPage;
                Repeater1.DataBind();
                RecordCount.Text = objPage.DataSourceCount.ToString();
                PageCount.Text = objPage.PageCount.ToString();
                Pageindex.Text = CurPage.ToString();
                Literal1.Text = PageList(objPage.PageCount, CurPage);

                FirstPage.NavigateUrl = Request.CurrentExecutionFilePath + "?id=" + id + "&page=1";
                PrevPage.NavigateUrl = Request.CurrentExecutionFilePath + "?id=" + id + "&page=" + (CurPage - 1);
                NextPage.NavigateUrl = Request.CurrentExecutionFilePath + "?id=" + id + "&page=" + (CurPage + 1);
                LastPaeg.NavigateUrl = Request.CurrentExecutionFilePath + "?id=" + id + "&page=" + objPage.PageCount.ToString();
                if (CurPage <= 1 && objPage.PageCount <= 1)
                {
                    FirstPage.NavigateUrl = "";
                    PrevPage.NavigateUrl = "";
                    NextPage.NavigateUrl = "";
                    LastPaeg.NavigateUrl = "";
                }
                if (CurPage <= 1 && objPage.PageCount > 1)
                {
                    FirstPage.NavigateUrl = "";
                    PrevPage.NavigateUrl = "";
                }
                if (CurPage >= objPage.PageCount)
                {
                    NextPage.NavigateUrl = "";
                    LastPaeg.NavigateUrl = "";
                }
            }
            catch (Exception error)
            {
                Response.Write(error.ToString());
            }
            finally
            {

            }
        }

        private string PageList(int Pagecount, int Pageindex)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<select style='width:50px' id=\"Page_Jump\" name=\"Page_Jump\" onchange=\"window.location='" + Request.CurrentExecutionFilePath + "?id=" + id + "&page='+ this.options[this.selectedIndex].value + '';\">");

            for (int i = 1; i <= Pagecount; i++)
            {
                if (Pageindex == i)
                    sb.Append("<option value='" + i + "' selected>" + i + "</option>");
                else
                    sb.Append("<option value='" + i + "'>" + i + "</option>");
            }
            sb.Append("</select>");
            return sb.ToString();
        }
    }
}